package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.MakeMoneyIndexResponseV3;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;

/**
 * Created by Alan on 2018/7/25.
 */

public class MakeMoneyV4Adapter extends BaseAdapter {

    private Activity mContext;
    private List<MakeMoneyIndexResponseV3.ContributionListBean> list;

    public MakeMoneyV4Adapter(Activity mContext, List<MakeMoneyIndexResponseV3.ContributionListBean> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public MakeMoneyIndexResponseV3.ContributionListBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        MyViewHolder myViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_makemoney_fun_v4_view, null);

            myViewHolder = new MyViewHolder();
            myViewHolder.itemFunRootLl = convertView.findViewById(R.id.itemFunRootLl);
            myViewHolder.itemFunIv = convertView.findViewById(R.id.itemFunIv);
            myViewHolder.itemFunTitleTx = convertView.findViewById(R.id.itemFunTitleTx);
            myViewHolder.itemFunMoneyTx = convertView.findViewById(R.id.itemFunMoneyTx);
            myViewHolder.itemFunRuleLl = convertView.findViewById(R.id.itemFunRuleLl);
            myViewHolder.itemFunLine = convertView.findViewById(R.id.itemFunLine);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }

        MakeMoneyIndexResponseV3.ContributionListBean temp = list.get(position);
        myViewHolder.itemFunIv.setImageURI(temp.getIcon());
        myViewHolder.itemFunMoneyTx.setText(temp.getMoney()+"元/个");
        myViewHolder.itemFunTitleTx.setText(temp.getTitle());
        myViewHolder.itemFunRuleLl.removeAllViews();
        List<String> listStr = temp.getDetail();
        for (int i=0;i<listStr.size();i++) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_makemoney_fun_item_v4_view, null);
            TextView num = itemView.findViewById(R.id.itemFunNumTx);
            TextView content = itemView.findViewById(R.id.itemFunContentTx);

            num.setText((i+1)+"、");
            String str = listStr.get(i);
            if (str.contains("、")) {
                content.setText(str.split("、")[1]);
            }
            content.setText(str);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                    , ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i!=0) {
                params.topMargin = (int) mContext.getResources().getDimension(R.dimen.wdp12);
            } else {
                params.topMargin = 0;
            }
            itemView.setLayoutParams(params);
            myViewHolder.itemFunRuleLl.addView(itemView);
        }

//        if (position == list.size()-1) {
//            myViewHolder.itemFunLine.setVisibility(View.GONE);
//        } else {
//            myViewHolder.itemFunLine.setVisibility(View.VISIBLE);
//        }

        return convertView;
    }

    public class MyViewHolder {
        private LinearLayout itemFunRootLl;
        private LinearLayout itemFunRuleLl;
        private SimpleDraweeView itemFunIv;
        private TextView itemFunTitleTx;
        private TextView itemFunMoneyTx;
        private View itemFunLine;
    }
}
